import time
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger


def task(name):
    print(f"Task executed! 秒数:{name}")


scheduler = BackgroundScheduler()

# method 1 cron
# 每天 10:30 执行
scheduler.add_job(task, CronTrigger.from_crontab("30 10 * * *"), args=["1030"])

# 每 30 秒执行一次（APScheduler 扩展语法）
scheduler.add_job(task, CronTrigger(second="*/30"), args=["30"])

# method 2
scheduler.add_job(task, 'interval', seconds=2, args=["2"])
scheduler.start()

# 防止主线程退出
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    scheduler.shutdown()
